package com.atguigu.staservice.service.impl;

import com.atguigu.commonutils.DateUtil;
import com.atguigu.staservice.client.UcenterClient;
import com.atguigu.staservice.entity.StatisticsDaily;
import com.atguigu.staservice.mapper.StatisticsDailyMapper;
import com.atguigu.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author fxy
 * @since 2022-05-16
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {

        //添加记录前删除表相同日期的数据
        QueryWrapper<StatisticsDaily> wrapper=new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);

        //远程调用接口 查询注册人数
        Integer register = ucenterClient.countRegister(day);

        //创建统计对象
        StatisticsDaily daily = new StatisticsDaily();
        Integer loginNum = RandomUtils.nextInt(100, 200);
        Integer videoViewNum = RandomUtils.nextInt(100, 200);
        Integer courseNum = RandomUtils.nextInt(100, 200);
        Integer orderNum = RandomUtils.nextInt(100, 200);
        daily.setRegisterNum(register); //注册人数
        daily.setDateCalculated(day);//统计日期
        daily.setLoginNum(loginNum);
        daily.setOrderNum(orderNum);
        daily.setVideoViewNum(videoViewNum);
        daily.setCourseNum(courseNum);
        daily.setDateCalculated(day);

        baseMapper.insert(daily);

    }

    @Override
    public Map<String, Object> getShouData(String type, String begin, String end) {
        //根据条件查询对应数据
        QueryWrapper<StatisticsDaily> wrapper=new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);//精准查询选择的字段

        List<StatisticsDaily> statisticsDailyList = baseMapper.selectList(wrapper);

        //因为返回有两部分数据，日期和日期对应数量
        //前端要求json数组结构,对应后端java是list集合
        //创建两个list集合，一个日期list，一个数量list
        List<String> date_calculatedList=new ArrayList<>();
        List<Integer> numDataList=new ArrayList<>();


        //遍历查询出来的所有数据的集合
        for (int i = 0; i <statisticsDailyList.size(); i++) {
            StatisticsDaily daily = statisticsDailyList.get(i);
            //封装日期list集合
            date_calculatedList.add(daily.getDateCalculated());
            //封装对应数量
            switch (type){
                case "login_num":
                    numDataList.add(daily.getLoginNum());
                    break;
                case "register_num":
                    numDataList.add(daily.getRegisterNum());
                    break;
                case "video_view_num":
                    numDataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    numDataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }

        Map<String,Object> map=new HashMap<>();
        map.put("date_calculatedList",date_calculatedList);
        map.put("numDataList",numDataList);

        return map;
    }

    @Override
    public Map<String, Object> showAllData(String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper=new QueryWrapper<>();


        if (begin==null || end ==null){
            String yesDate = DateUtil.formatDate(DateUtil.addDays(new Date(), -1));
            String sevenDate = DateUtil.formatDate(DateUtil.addDays(new Date(), -7));
            wrapper.between("date_calculated",sevenDate,yesDate);
            wrapper.orderByAsc("date_calculated");
        }else {
            wrapper.between("date_calculated",begin,end);
        }
        wrapper.select("date_calculated","register_num","login_num","video_view_num","course_num","order_num");//精准查询选择的字段

        List<StatisticsDaily> statisticsDailyList = baseMapper.selectList(wrapper);

        List<String> date_calculatedList=new ArrayList<>();
        List<Integer> register_numList=new ArrayList<>();
        List<Integer> login_numList=new ArrayList<>();
        List<Integer> video_view_numList=new ArrayList<>();
        List<Integer> course_numList=new ArrayList<>();
        List<Integer> order_numList=new ArrayList<>();

        for (int i = 0; i < statisticsDailyList.size(); i++) {
            StatisticsDaily daily = statisticsDailyList.get(i);
            date_calculatedList.add(daily.getDateCalculated());
            register_numList.add(daily.getRegisterNum());
            login_numList.add(daily.getLoginNum());
            video_view_numList.add(daily.getVideoViewNum());
            course_numList.add(daily.getCourseNum());
            order_numList.add(daily.getOrderNum());
        }

        Map<String,Object> map=new HashMap<>();
        map.put("date_calculatedList",date_calculatedList);
        map.put("register_numList",register_numList);
        map.put("login_numList",login_numList);
        map.put("video_view_numList",video_view_numList);
        map.put("course_numList",course_numList);
        map.put("order_numList",order_numList);
        return map;
    }

    @Override
    public Map<String, Object> getAllData() {
        Map<String,Object> map=new HashMap<>();
        //用户数量
        Integer userCount = baseMapper.getUserCount();

        //交易完成订单量
        Integer orderCount = baseMapper.getOrderCount();

        //已发布课程数量
        Integer courseCount = baseMapper.getCourseCount();

        //讲师数量
        Integer teacherCount = baseMapper.getTeacherCount();

        Integer viewCount = RandomUtils.nextInt(20, 50);

        map.put("userCount",userCount);
        map.put("orderCount",orderCount);
        map.put("courseCount",courseCount);
        map.put("teacherCount",teacherCount);
        map.put("viewCount",viewCount);


        //7天数据
        Map<String, Object> sevenMap = showAllData(null, null);
        map.put("sevenMap",sevenMap);
        return map;
    }
}
